#include once "mud-gui/ui.bi"
#include once "mud-gui/widgets/GridLayout.bi"
#include once "mud-gui/widgets/Label.bi"
#include once "mud-gui/widgets/Canvas.bi"

'Example program testing GridLayout and Canvas with
'a Custom Background! Neato.

sub CustomBack( byval x_ as any ptr ) 'doesn't use the data var

static a as single
static as integer r, g, b
static scr as uinteger ptr
scr = screenptr
a = timer
 
'screenlock - unnecessary, ran in the draw loop.
 
for y as integer = 0 to 239    
    for x as integer = 0 to 319
 
        R = 127 + 127 * cos(x / (64 + 32 * cos(y / 128 + a))) * cos(y / (96 + 16 * sin(x / 64 - a)))
        G = 127 + (96*sin(a*3)) * cos(x / (96 + 16 * cos(y / 64 ))) * cos(y / (64 + 16  * sin(x / 64 + a)) )
        B = 127 + (64*cos(a)) * cos(( ((R*sin(a))/64)^2 + ((G*cos(a))/64)^2))
        scr[x+y*320] = RGB(R,G,B)
 
    next
next

end sub

using ui

dim as GridLayout ptr mw
dim as label ptr label1
dim as Canvas ptr cv

if initScreen("Custom Background Example") = 0 then
	print "unable to initialize screen."
	end 23
end if

mw = new GridLayout
mw->setBgColor = RGBA(255,0,255,255)
label1 = new label
label1->setText = "Trippy Man, Draw Something!"
label1->setAlignment = alignment.center 

cv = new Canvas
cv->setDrawable = 1 'defaults to not allowing the mouse to draw
cv->setFgColor = rgb(0,255,0)
cv->setBgColor = rgb(255,0,255)
cv->clear() 'actually draws the canvas transparent
mw->sizeGrid(5,5)

ConsolePrint( "1: " & mw->placeWidget( toBaseWidget(label1), 1, 1, 5, 1 ) )
ConsolePrint( "2: " & mw->placeWidget( toBaseWidget(cv), 1, 2, 5, 5 ) )

'Canvas widget isn't sized until it is placed in a layout, so a draw call before
'here would fail.
cv->drawCircle( 100,100,50 )
cv->drawFilledCircle( 200,100,10,rgb(255,0,0) )
cv->drawLine( 1, 1, 20, 20 )
cv->drawBox( 20, 20, 30, 30 )
cv->drawFilledBox( 150,10, 160, 20 )

setCustomBackgroundProvider( @CustomBack, cast( any ptr,0) )

end RunGUI( toBaseWidget(mw) )
